package mode.singleton.lhs;

/**
 * 懒汉式
 *
 * @author xjx
 * @date 2023/7/17 15:52
 */
public class SingletonDemoLhs {
    //volatile修饰，禁止指令重拍，为了后续线程安全
    private volatile static SingletonDemoLhs instance = null;

    //构造方法私有化
    private SingletonDemoLhs() {
    }

    //在对外暴露的方法中实例化对象
    public static SingletonDemoLhs getInstance() {
        if (instance == null) {
            synchronized (SingletonDemoLhs.class) {
                if (instance == null) {
                    instance = new SingletonDemoLhs();
                }
            }
        }
        return instance;
    }
}
